package com.bw.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.finance.conf.OrderMqConfig;
import com.bw.finance.conf.ResponseResult;
import com.bw.finance.eneity.invest.BiaoInfo;
import com.bw.finance.eneity.invest.EbankInvestLog;
import com.bw.finance.eneity.locana.EbankBidInfo;
import com.bw.finance.eneity.locana.EbankLoanInfo;
import com.bw.finance.eneity.user.EbankInvestor;
import com.bw.finance.mapper.EbankInvestLogMapper;
import com.bw.finance.service.IEbankBidInfoService;
import com.bw.finance.service.IEbankInvestLogService;
import com.bw.finance.service.IEbankInvestorService;
import com.bw.finance.service.IEbankLoanInfoService;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lizhengqian
 * @since 2020-08-25
 */
@Service
public class EbankInvestLogServiceImpl extends ServiceImpl<EbankInvestLogMapper, EbankInvestLog> implements IEbankInvestLogService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private EbankInvestLogMapper investLogMapper;

    @Autowired
    private IEbankInvestLogService investLogService;

    @Autowired
    private IEbankInvestorService investorService;

    @Override
    public ResponseResult touzi(Long uid, BigDecimal balance, BigDecimal money, BiaoInfo biaoInfo) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            //判断用户余额是否充足
            if(balance.compareTo(money)==-1){
                result.setSuccess(false);
                result.setMessage("账户余额不足，请先进行充值！！！");
                return result;
            }
            //判断是否到达开售时间
            if(biaoInfo.getOpenTime().getTime()>System.currentTimeMillis()){
                result.setSuccess(false);
                result.setMessage("当前标没有到达开售时间！！！");
                return result;
            }
            //判断是否到达结束时间
            if(biaoInfo.getRaiseType()==1){
                Calendar cal = Calendar.getInstance();
                cal.setTime(biaoInfo.getOpenTime());
                cal.add(Calendar.DAY_OF_MONTH,biaoInfo.getRaiseNum());
                if(cal.getTime().getTime()<System.currentTimeMillis()){
                    result.setSuccess(false);
                    result.setMessage("当前标募集时间已结束！！！");
                    return result;
                }
            }
            //一个标只能被一个用户投资一次
            Long num = redisTemplate.opsForSet().add("biao:" + biaoInfo.getId(),uid);
            if(num<=0){
                result.setSuccess(false);
                result.setMessage("你已经买过此标,不能重复购买！！！");
                return result;
            }
            //判断投资的金额是否大于可投金额
            String totalmoney = stringRedisTemplate.opsForValue().get("amount:" + biaoInfo.getId());
            BigDecimal totalmon = new BigDecimal(totalmoney);
            if(money.compareTo(totalmon)==1){
                result.setSuccess(false);
                result.setMessage("投资金额过大，当前标可投金额为"+totalmoney+"!!!");
                return result;
            }
            totalmon=totalmon.subtract(money);
            stringRedisTemplate.opsForValue().set("amount:"+biaoInfo.getId(),String.valueOf(totalmon));
            //进行投资
            Map map=new HashMap<>();
            map.put("bid",biaoInfo.getId());
            map.put("money",money);
            map.put("uid",uid);
            rabbitTemplate.convertAndSend(OrderMqConfig.EX_SECKILL,OrderMqConfig.RK_ORDER,map);
            result.setMessage("正在投标中......");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("投资失败");
        }
        return result;
    }


    @Override
    public ResponseResult getTouziList(Long id) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper<EbankInvestLog> queryWrapper=new QueryWrapper();
            queryWrapper.lambda().eq(EbankInvestLog::getBid,id);
            List<EbankInvestLog> list = investLogService.list(queryWrapper);
            List<EbankInvestLog> list1 = new ArrayList<>();
            for (EbankInvestLog eil:list) {
                EbankInvestor investor = investorService.getById(eil.getInvestorId());
                eil.setFullName(investor.getFullName());
                list1.add(eil);
            }
            result.setResult(list1);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败!");
            e.printStackTrace();
        }
        return result;
    }
}
